/* Fix CSS
 * http://code.google.com/p/html5andcss3/
 * Author: kael http://weibo.com/loskael
 */
package los.kael.file;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 读取文件到StringBuffer中
 * 
 * @author kael
 * 
 */
public class FileReaderToStringBuffer {
	private String charset = "UTF-8";
	private String fileName = null;
	private StringBuffer sb = new StringBuffer();
	private InputStreamReader in = null;
	private int c;

	/**
	 * 指定文件名
	 * 
	 * @param fileName
	 */
	public FileReaderToStringBuffer(String fileName) {
		this.fileName = fileName;
	}

	/**
	 * 指定文件名和编码
	 * 
	 * @param fileName
	 * @param charset
	 */
	public FileReaderToStringBuffer(String fileName, String charset) {
		this.fileName = fileName;
		this.charset = charset;
	}

	/**
	 * 输出到StringBuffer中
	 * 
	 * @return
	 */
	public StringBuffer getSb() {
		try {
			in = new InputStreamReader(new FileInputStream(fileName), charset);
			while ((c = in.read()) != -1) {
				sb.append((char) c);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		sb = new StringBuffer(sb.toString().trim());
		return sb.append("\r\n\r\n");
	}
}
